package com.example.sell.controller;

import com.example.sell.bean.ProductInfo;
import com.example.sell.common.ResultVOUtil;
import com.example.sell.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 * 商品信息
 *
 * @author ouyangzhixiang
 * @date 2018/6/28
 */
@RequestMapping("/product/")
@RestController
public class ProductInfoContorller {
    private final ProductService productService;

    @Autowired
    public ProductInfoContorller(ProductService productService) {
        this.productService = productService;
    }

    /**
     * 查询所有商品
     *
     * @return
     */
    @GetMapping("list")
    public ResultVOUtil<List<ProductInfo>> findAll() {
        ResultVOUtil<List<ProductInfo>> productInfoList = productService.findAll();
        return productInfoList;
    }

    /**
     * 查询单个商品信息
     *
     * @param productId
     * @return
     */
    @GetMapping("find_one/{productId}")
    public ResultVOUtil<Optional<ProductInfo>> findOne(@PathVariable("productId") String productId) {
        ResultVOUtil<Optional<ProductInfo>> resultProductInfo = productService.findOne(productId);
        return resultProductInfo;
    }

    /**
     * 查询所有在架商品信息
     *
     * @return
     */
    @GetMapping("find_up")
    public List<ProductInfo> findUp() {
        return productService.findUp();
    }

    /**
     * 添加更新商品信息
     *
     * @param productInfo
     * @return
     */
    @PostMapping("add_product")
    public ResultVOUtil<ProductInfo> addProduct(ProductInfo productInfo) {
        return productService.save(productInfo);
    }

    /**
     * 实现商品上架
     * @param productId
     * @return
     */
    @RequestMapping("shelf")
    public ResultVOUtil<ProductInfo> shelf(@RequestParam("productId") String productId,@RequestParam("productStatus") Integer productStatus ){
        return productService.shelf(productId,productStatus);
    }

    /**
     * 实现商品下架
     * @param productId
     * @return
     */
    @RequestMapping("dropoff")
    public ResultVOUtil<ProductInfo> dropoff(@RequestParam("productId") String productId,@RequestParam("productStatus") Integer productStatus){
        return productService.dropoff(productId,productStatus);
    }
}
